import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class InputDialog extends StatefulWidget {
  final int type;
  final String desc;

  InputDialog(this.type, this.desc);

  @override
  State<InputDialog> createState() => _InputDialogState();
}

class _InputDialogState extends State<InputDialog> {
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.text = widget.desc;
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          FocusManager.instance.primaryFocus?.unfocus();
        },
        child: Align(
          alignment: const Alignment(0, -0.3),
          child: Container(
            width: 300,
            padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 15),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(15)),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextField(
                  controller: _controller,
                  keyboardType: widget.type == 0
                      ? TextInputType.text
                      : TextInputType.phone,
                  decoration: InputDecoration(
                      hintText: widget.type == 0 ? "联系人姓名" : "联系人手机号",
                      border: const OutlineInputBorder(),
                      enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.grey.shade200)),
                      focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.grey.shade400),
                          borderRadius: BorderRadius.circular(15))),
                ),
                const SizedBox(
                  height: 15,
                ),
                InkWell(
                  onTap: () {
                    String desc = _controller.text;
                    if (desc.isEmpty) {
                      if (widget.type == 0) {
                        EasyLoading.showInfo("请输入姓名");
                      } else {
                        EasyLoading.showInfo("请输入手机号");
                      }
                      return;
                    }

                    Navigator.of(context).pop(desc);
                  },
                  child: Container(
                    width: 100,
                    height: 50,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        gradient: const LinearGradient(
                            colors: [Color(0xFFFF8343), Color(0xFFFF6C72)])),
                    child: const Text(
                      "确定",
                      style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.w600,
                          fontSize: 16),
                    ),
                  ),
                ),
                const SizedBox(
                  height: 15,
                ),
                InkWell(
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                  child: Container(
                    width: 40,
                    height: 40,
                    decoration: const BoxDecoration(
                        color: Colors.black12, shape: BoxShape.circle),
                    child: const Icon(Icons.close),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Future showInputDialog(BuildContext context, int type, String desc) {
  return showDialog(context: context, builder: (_) => InputDialog(type, desc));
}
